package com.th.dao;

import com.th.entity.Adm;
import com.th.entity.Obj;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

public class AdmDaoImp implements AdmDao{
    JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;

    }
    @Override
    public void insertAdm(Adm adm)
    {
        String sql = "insert into adm values(?,?,?,?)";
        jdbcTemplate.update(sql,adm.getOpenid(),adm.getWork_id(),adm.getName(),adm.getPhone());
    }

    @Override
    public void deleteAdm(String openid) {
        String sql = "delete from adm where openid=?";
        jdbcTemplate.update(sql,openid);
    }

    @Override
    public void updateAdm(Adm adm) {
        String sql = "update adm set phone=? where openid=?";
        jdbcTemplate.update(sql,adm.getPhone(),adm.getOpenid());
    }

    @Override
    public Adm selectAdm(String openid) {
        String sql = "select * from adm where openid=?";
        Adm adm = null;
        try {
            adm = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Adm>(Adm.class), openid);
        } catch (Exception e)
        {
            return null;
        }
        return adm;
    }

    @Override
    public List<Adm> selectAdms() {
        String sql = "select * from adm";
        return jdbcTemplate.query(sql,new RowMapper<Adm>(){
            @Override
            public Adm mapRow(ResultSet rs, int rowNum) throws SQLException {
                Adm adm = new Adm();
                adm.setOpenid(rs.getString("openid"));
                adm.setName(rs.getString("name"));
                adm.setPhone(rs.getString("phone"));
                adm.setWork_id(rs.getInt("work_id"));
                return adm;
            }
        });
    }

}
